﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Backend.JYD.EFDAL.DAL
{
    public class BidStatDAL : IDAL.Bid.IBidStat
    {
        #region IBidStat 成员

        public long AddBidStat(Model.Bid.BidStat stat)
        {
            using (JydDataContext db = new JydDataContext())
            {
                db.BidStats.Add(stat);
                db.SaveChanges();
                return stat.UserId;
            }
        }

        public bool UpdateBidStat(Model.Bid.BidStat stat)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidStats.Where(p => p.UserId == stat.UserId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.BiddingNum = stat.BiddingNum;
                    temp.PayNum = stat.PayNum;
                    temp.FinishNum = stat.FinishNum;
                    temp.OverdueNum = stat.OverdueNum;
                    temp.RepayNum = stat.RepayNum;
                    temp.BiddingAmount = stat.BiddingAmount;
                    temp.ComingInterest = stat.ComingInterest;
                    temp.ComingCapital = stat.ComingCapital;
                    temp.TotalCapital = stat.TotalCapital;

                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public Model.Bid.BidStat GetBidStatByUserId(long userId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidStats.Where(p => p.UserId == userId);
                return query.FirstOrDefault();
            }
        }

        public IList<Model.Bid.BidStat> GetBidStatByUserIds(long[] userIds)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidStats.Where(p => userIds.Contains(p.UserId));
                return query.ToList();
            }
        }
        #endregion
    }
}
